import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import connfig from '../config.js'

const localEvent = function(item) {
  this.get = function() {
    return JSON.parse(localStorage.getItem(item));
  }
  this.set = function(obj) {
    localStorage.setItem(item, JSON.stringify(obj));
  }
  this.clear = function() {
    localStorage.removeItem(item);
  }
}
const local = new localEvent(connfig.appTheme);

const getters = {
  //购物车列表
  carList(state) {
    // 初始化全选状态
    if (state.all_selsect) {
      state.cart.forEach((item) => {
        item.selected = true
      })
    }
    return state.cart
  },
  //选中商品的金额
  allMoney(state) {
    let all_money = 0
    state.cart.forEach((item) => {
      if (item.selected) {
        all_money += item.num * item.sellPrice
      }
    })
    return state.all_money = all_money
  },
  allSelsect(state) {
    return state.all_selsect;
  }

}

const state = local.get() ||  {
  cart: [
    // {id: 1, title:'苹果', price: 200, quantity : 1, image: ''},  //示例
  ],
  all_money: 0, //总价
  all_selsect: true, //全选状态
  cartCounter: 0
}

const mutations = {

  //添加商品
  ADDGOODS(state, data) {
    let datas = { //传入的商品属性
      title: data.title,
      image: data.image,
      num: 1,
      sellPrice: data.price,
      marketPrice: data.price,
      selected: true,
      productId: data.id
    }
    let index = -1
    index = state.cart.findIndex(item => { //返回元素的索引位置，如果没有符合条件的元素返回 -1
      return item.productId == data.id
    })
    if (index == -1) { //返回-1说明购物车里没有这个商品，则push进去，如果返回下标，则让此商品的数量加1
      state.cart.push(datas)
    } else {
      state.cart[index].num++
    }
    state.cartCounter++;
    local.set(state);
  },
  //购物车页面数量加减
  ADDNUM(state, productId) {
    let index = state.cart.findIndex(item => {
      return item.productId == productId
    })
    state.cartCounter++;
    state.cart[index].num++
    local.set(state);
  },
  //购物车页面数量加减
  REDUCENUM(state, productId) {
    let index = state.cart.findIndex(item => {
      return item.productId == productId
    })
    if (state.cart[index].num <= 1) {
      state.cart[index].num = 1
    } else {
      state.cartCounter--;
      state.cart[index].num--
    }
    local.set(state);
  },
  //购物车单选
  SELECT(state, productId) {
    let index = state.cart.findIndex(item => {
      return item.productId == productId
    })
    state.cart[index].selected = !state.cart[index].selected
    let flag = state.cart.some((item) => {
      return item.selected == false
    })
    if (!flag) { //如果全部选中，则全选状态也改变
      state.all_selsect = true
    } else {
      state.all_selsect = false
    }
    local.set(state);
  },
  // 购物车全选
  SELECTALL(state) {
    state.all_selsect = !state.all_selsect
    if (state.all_selsect) {
      state.cart.forEach((item) => {
        item.selected = true
      })
    } else {
      state.cart.forEach((item) => {
        item.selected = false
      })
    }
    local.set(state);
  },

  DEL(state, productId) {


    state.cartCounter--;
    if (state.cartCounter < 0) {
      state.cartCounter = 0
    }
    state.cart.some((val, index, Goods) => {
      if (val.productId === productId) {
        val.quantity--;
        Goods.splice(index, 1);
        return true;
      }
    })
    local.set(state);
  },

  //删除选中的商品，注意这里不能使用数组删除的方法，索引值会有问题
  CLEAR_SELECTED(state) {


    var list = [];
    state.cart.forEach((item, index) => {
      if (!item.selected) {
        list.push(item)
      }else{
        state.cartCounter = state.cartCounter - item.num;
      }
    })

    state.cart = list

    local.set(state);

  },

}

const actions = {
  // 第一次加入购物车
  addGoods({ commit }, data) {
    commit('ADDGOODS', data)
  },

  // 添加
  addNum({ commit }, id) {
    commit('ADDNUM', id)
  },
  // 减少
  jianNum({ commit }, id) {
    commit('REDUCENUM', id)
  },
  // 选择
  cartSelect({ commit }, id) {
    commit('SELECT', id)
  },
  // 选择全部
  cartSelectAll({ commit }) {
    commit('SELECTALL')
  },
  deleteGoodsFromCart({ commit }, id) {
    commit('DEL', id)
  },
  clearSelected({ commit }) {
    commit('CLEAR_SELECTED')
  }

}

export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions
})
